package com.bee.common.config;

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;

import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
@Configuration
@ConfigurationProperties("application.properties")
public class MongoDataSourceConfig extends AbstractMongoConfiguration{
	
    @Value("${spring.data.mongodb.username}")
    private String mongoName;

    @Value("${spring.data.mongodb.host}")
    private String mongoHost;

    @Value("${spring.data.mongodb.port}")
    private String mongoPort;
    
    @Value("${spring.data.mongodb.password}")
    private String mongoPassWord;

    @Value("${spring.data.mongodb.database}")
    private String mongoDB;

	@Override
	protected String getDatabaseName() {
		return this.mongoDB;
	}

	@Override
	@Bean
	public Mongo mongo() throws Exception {
		ServerAddress serverAdress = new ServerAddress(mongoHost, Integer.valueOf(mongoPort));  
        MongoCredential credential = MongoCredential.createCredential(mongoName, mongoDB, mongoPassWord.toCharArray());  
        Mongo mongo = new MongoClient(serverAdress, Arrays.asList(credential));   
        return mongo;
	}

}
